package edu.sctu.wangfuxin.day20150614.homework_IO;


import java.io.*;

/**
 * Created by user06 on 2015/6/14.
 */
public class IO_Homework {
    public static final String PATH = "D:\\TestIO\\TestFileReader.txt";
    public static final String NEWPATH = "D:\\TestIO\\TestFileReader1.txt";

    /**
     * 字符读取文件
     */
    public void fileReader() {
        int i;
        try {
            FileReader fileReader = new FileReader(new File(PATH));
            try {
                while ((i = fileReader.read()) != -1) {
                    System.out.print((char) i);
                }
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 以字符数组形式写文件
     */
    public void fileWriter() {
        char[] chars = new char[1024];
        int count;
        try {
            FileWriter fileWriter = new FileWriter(NEWPATH);
            FileReader fileReader1 = new FileReader(PATH);
            while ((count = fileReader1.read(chars)) != -1) {
                fileWriter.write(chars, 0, count);
            }
            fileReader1.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 字符转换字节输出
     */
    public void outputChar() {
        int b = -1;
        byte[] a = new byte[1024];
        try {
            BufferedReader bf = new BufferedReader(new FileReader(NEWPATH));
            System.out.println("\n字节输出：");
            try {
                while ((bf.read()) != -1) {
                    ByteArrayInputStream bais = new ByteArrayInputStream(bf.readLine().getBytes());
                    while ((b = bais.read()) != -1) {
                        System.out.println(b);
                    }
                }
                bf.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    /**
     * main 函数
     *
     * @param args
     */

    public static void main(String[] args) {
        IO_Homework io_homework = new IO_Homework();
        io_homework.fileReader();
        io_homework.fileWriter();
        io_homework.outputChar();
    }


}
